#ifndef GROUPSMODEL_H
#define GROUPSMODEL_H

#include <QAbstractListModel>
#include <QQmlEngine>

#include "../global/xnamespace.h"
#include "submodel/GroupModel.h"

X_BEGIN_NAMESPACE

// 群组模型
class GroupsModel : public QAbstractListModel
{
    Q_OBJECT

public:
    GroupsModel(QObject* parent = 0);

    // Singleton supports
    static GroupsModel& instance();
    static const char* qmlName();
    static QObject *qmlInstance(QQmlEngine* engine, QJSEngine *jsEngine);

public:
    virtual int rowCount(const QModelIndex &parent) const override;
    virtual QVariant data(const QModelIndex &index, int role) const override;
    virtual QHash<int, QByteArray> roleNames() const override;

    void loadDataFromDb();
    void houseKeep();

public:
    Q_INVOKABLE GroupModel &addGroup(const QString &id, const QString &name, const QString &photo, const QDateTime createTime);
    Q_INVOKABLE GroupModel &createGroup(const QString &id, const QString &name);
    Q_INVOKABLE QObject *findGroup(const QString& id) const;

private:
    static GroupsModel *createInstance();
    static bool groupLessThan(GroupModel* g1, GroupModel* g2);

private:
    QHash<int, QByteArray> m_roleNames;

    QList<GroupModel *> m_groups;
    QMap<QString, GroupModel *> m_groupsById;

private:
    static const char* ROLE_NAMES[];
    static const char READ_GROUPS[];
};

X_END_NAMESPACE

#endif // GROUPMODEL_H
